﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hr.IDAL;
using Hr.Model;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Data.Common.CommandTrees;
using System.Diagnostics;
using System.IO;
using Microsoft.EntityFrameworkCore.Query.Internal;

namespace Hr.DAL
{
    public partial class ProcessDAL : BaseDAL<Process_List>, IProcess_ListDAL
    {
        //创建EF对象
        private DbContext db = DbContextFactory.Create();

        #region 获取流程管理页面信息(自己申请的流程)
        /// <summary>
        /// 获取流程管理页面信息(自己申请的流程)
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程管理</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/15</create>
        /// <updata>null</updata>
        public IEnumerable<object> SendProcessList(int fk_User)
        {
            return (from ProcessList in db.Set<Process_List>()
                    join UserList in db.Set<User_List>() on ProcessList.fk_User equals UserList.pk_User
                    join SysTypeID in db.Set<Sys_TypeID>() on ProcessList.TypeID equals SysTypeID.ID
                    join SysStatusID in db.Set<Sys_StatusID>() on ProcessList.StatusID equals SysStatusID.ID
                    where ProcessList.fk_User == fk_User && ProcessList.CheckUser == 0 && SysTypeID.TableName.Equals("Process_List") && SysStatusID.TableName.Equals("Process_List")
                    select new
                    {
                        ProcessList.pk_Process,
                        SysTypeID.TypeName_CHS,
                        ProcessList.Title,
                        UserList.FullName,
                        ProcessList.CreateDate,
                        SysStatusID.StatusName_CHS,
                        SysStatusID.Visual,
                        ProcessList.FilePath,
                    }).ToList();


        }
        #endregion

        #region 流程管理添加下拉框绑定
        /// <summary>
        /// 流程管理添加下拉框绑定
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程管理</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/17</create>
        /// <updata>null</updata>

        public IEnumerable<Sys_TypeID> SelectTypeBinding()
        {
            return db.Set<Sys_TypeID>().Where(n => n.TableName == "Process_List").ToList();
        }
        #endregion

        #region 流程管理添加
        /// <summary>
        /// 流程管理添加
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程管理</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/17</create>
        /// <updata>null</updata>
        public void AddProcessList(Process_List PList, int fk_User)
        {
            //申请人
            PList.fk_User = fk_User;
            //申请人部门
            PList.fk_Department = db.Set<User_List>().SingleOrDefault(n => n.pk_User == fk_User).fk_Department;
            //状态默认为处理中
            PList.StatusID = 0;
            PList.Note = "";
            PList.ImagePath = "";
            PList.FilePath = "";
            PList.CheckUser = 0;
            //申请人公司
            PList.fk_Company = 0;
            PList.CreateUser = fk_User;
            //创建时间
            PList.CreateDate = DateTime.Now;
            //最后修改用户
            PList.ModifyUser = fk_User;
            //最后修改时间
            PList.ModifyDate = DateTime.Now;
            //是否批准
            PList.IsCheck = false;
            //批准时间
            PList.CheckDate = DateTime.Now;
            //批准人部门默认为0
            PList.CheckDepartment = 0;
            PList.AtUser = "";
            PList.Tags = "";
            PList.ParentID = 0;

            db.Set<Process_List>().Add(PList);
            if (db.SaveChanges() > 0)
            {
                //加进去以后
                int pk_Process = PList.pk_Process;
                int TypeID = int.Parse(PList.TypeID.ToString());

                string prolist = (from ProcessList in db.Set<Process_List>()
                                  join SysTypeID in db.Set<Sys_TypeID>() on ProcessList.TypeID equals SysTypeID.ID
                                  where SysTypeID.TableName.Equals("Process_List") && SysTypeID.ID == TypeID
                                  select SysTypeID).FirstOrDefault().Description_CHT.ToString();
                List<int> typeid = new List<int>();
                if (prolist.IndexOf("|") != -1)
                {
                    typeid = prolist.Split('|').Select(n => int.Parse(n.ToString())).ToList();

                }
                else
                {
                    int id = int.Parse(prolist);
                    //如果只有一个就不用循环添加，只需要添加一个就行了
                    typeid.Add(id);
                }
                foreach (int item in typeid)
                {
                    Process_List p = new Process_List();
                    //申请人
                    p.fk_User = fk_User;
                    p.TypeID = PList.TypeID;
                    p.Description = PList.Description;
                    p.Title = PList.Title;
                    //申请人部门
                    p.fk_Department = db.Set<User_List>().SingleOrDefault(n => n.pk_User == fk_User).fk_Department;
                    //状态默认为处理中
                    p.StatusID = 0;
                    p.Note = "";
                    p.ImagePath = "";
                    p.FilePath = "";
                    p.CheckUser = item;
                    //申请人公司
                    p.fk_Company = 0;
                    p.CreateUser = fk_User;
                    //创建时间
                    p.CreateDate = DateTime.Now;
                    //最后修改用户
                    p.ModifyUser = fk_User;
                    //最后修改时间
                    p.ModifyDate = DateTime.Now;
                    //是否批准
                    p.IsCheck = false;
                    //批准时间
                    p.CheckDate = DateTime.Now;
                    //批准人部门默认为0
                    p.CheckDepartment = db.Set<User_List>().SingleOrDefault(n => n.pk_User == item).fk_Department; ;
                    p.AtUser = "";
                    p.Tags = "";
                    p.ParentID = PList.pk_Process;

                    db.Set<Process_List>().Add(p);
                }
            }
        }
        #endregion

        #region 根据Title获取流程管理页面信息(自己申请的流程)
        /// <summary>
        /// 根据Title获取流程管理页面信息(自己申请的流程)
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程管理</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/15</create>
        /// <updata>null</updata>

        public IEnumerable<object> TitleSelectProcessList(int fk_User, string Title)
        {
            return (from ProcessList in db.Set<Process_List>()
                    join UserList in db.Set<User_List>() on ProcessList.fk_User equals UserList.pk_User
                    join SysTypeID in db.Set<Sys_TypeID>() on ProcessList.TypeID equals SysTypeID.ID
                    join SysStatusID in db.Set<Sys_StatusID>() on ProcessList.StatusID equals SysStatusID.ID
                    where ProcessList.fk_User == fk_User && ProcessList.CheckUser == 0 && SysTypeID.TableName.Equals("Process_List") && SysStatusID.TableName.Equals("Process_List") && ProcessList.Title.Contains(Title)
                    select new
                    {
                        ProcessList.pk_Process,
                        SysTypeID.TypeName_CHS,
                        ProcessList.Title,
                        UserList.FullName,
                        ProcessList.CreateDate,
                        SysStatusID.StatusName_CHS,
                        SysStatusID.Visual,
                        ProcessList.FilePath,
                    }).ToList();
        }
        #endregion

        #region 流程审核页面(获取需要自己审核的流程)
        /// <summary>
        /// 流程审核页面(获取需要自己审核的流程)
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程审核</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/15</create>
        /// <updata>null</updata>

        public IEnumerable<object> SendAuditProcessList(int fk_User)
        {
            return (from ProcessList in db.Set<Process_List>()
                    join UserList in db.Set<User_List>() on ProcessList.fk_User equals UserList.pk_User
                    join SysTypeID in db.Set<Sys_TypeID>() on ProcessList.TypeID equals SysTypeID.ID
                    join SysStatusID in db.Set<Sys_StatusID>() on ProcessList.StatusID equals SysStatusID.ID
                    where ProcessList.CheckUser == fk_User && SysTypeID.TableName.Equals("Process_List") && SysStatusID.TableName.Equals("Process_List")
                    select new
                    {
                        ProcessList.ParentID,
                        ProcessList.CheckUser,
                        SysTypeID.TypeName_CHS,
                        ProcessList.Title,
                        UserList.FullName,
                        ProcessList.CreateDate,
                        SysStatusID.StatusName_CHS,
                        SysStatusID.Visual,
                        ProcessList.FilePath,
                    }).ToList();
        }
        #endregion

        #region 根据Title获取流程审核页面(获取需要自己审核的流程)
        /// <summary>
        /// 根据Title获取流程审核页面(获取需要自己审核的流程)
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程审核</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/15</create>
        /// <updata>null</updata>

        public IEnumerable<object> TitleSelectAuditProcessList(int fk_User, string Title)
        {
            return (from ProcessList in db.Set<Process_List>()
                    join UserList in db.Set<User_List>() on ProcessList.fk_User equals UserList.pk_User
                    join SysTypeID in db.Set<Sys_TypeID>() on ProcessList.TypeID equals SysTypeID.ID
                    join SysStatusID in db.Set<Sys_StatusID>() on ProcessList.StatusID equals SysStatusID.ID
                    where ProcessList.CheckUser == fk_User && SysTypeID.TableName.Equals("Process_List") && SysStatusID.TableName.Equals("Process_List") && ProcessList.Title.Contains(Title)
                    select new
                    {
                        ProcessList.pk_Process,
                        SysTypeID.TypeName_CHS,
                        ProcessList.Title,
                        UserList.FullName,
                        ProcessList.CreateDate,
                        SysStatusID.StatusName_CHS,
                        SysStatusID.Visual,
                        ProcessList.FilePath,
                    }).ToList();
        }
        #endregion

        #region 获取流程汇总页面(全部流程)
        /// <summary>
        /// 获取流程汇总页面(全部流程)
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程汇总</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/15</create>
        /// <updata>null</updata>

        public IEnumerable<object> SummaryProcessList(int fk_User)
        {
            return (from ProcessList in db.Set<Process_List>()
                    join UserList in db.Set<User_List>() on ProcessList.fk_User equals UserList.pk_User
                    join SysTypeID in db.Set<Sys_TypeID>() on ProcessList.TypeID equals SysTypeID.ID
                    join SysStatusID in db.Set<Sys_StatusID>() on ProcessList.StatusID equals SysStatusID.ID
                    where ProcessList.CheckUser == 0 && SysTypeID.TableName.Equals("Process_List") && SysStatusID.TableName.Equals("Process_List")
                    select new
                    {
                        ProcessList.pk_Process,
                        SysTypeID.TypeName_CHS,
                        ProcessList.Title,
                        UserList.FullName,
                        ProcessList.CreateDate,
                        SysStatusID.StatusName_CHS,
                        SysStatusID.Visual,
                        ProcessList.FilePath,
                    }).ToList();
        }
        #endregion

        #region 根据Title获取流程汇总页面
        /// <summary>
        /// 根据Title获取流程汇总页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns>流程汇总</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/15</create>
        /// <updata>null</updata>

        public IEnumerable<object> TitleSelectSummaryProcessList(int fk_User, string Title)
        {
            return (from ProcessList in db.Set<Process_List>()
                    join UserList in db.Set<User_List>() on ProcessList.fk_User equals UserList.pk_User
                    join SysTypeID in db.Set<Sys_TypeID>() on ProcessList.TypeID equals SysTypeID.ID
                    join SysStatusID in db.Set<Sys_StatusID>() on ProcessList.StatusID equals SysStatusID.ID
                    where ProcessList.CheckUser == 0 && SysTypeID.TableName.Equals("Process_List") && SysStatusID.TableName.Equals("Process_List") && ProcessList.Title.Contains(Title)
                    select new
                    {
                        ProcessList.pk_Process,
                        SysTypeID.TypeName_CHS,
                        ProcessList.Title,
                        UserList.FullName,
                        ProcessList.CreateDate,
                        SysStatusID.StatusName_CHS,
                        SysStatusID.Visual,
                        ProcessList.FilePath,
                    }).ToList();
        }
        #endregion

        #region 流程汇总页面删除功能
        /// <summary>
        /// 流程汇总页面删除功能
        /// </summary>
        /// <param name="id">pk_Process</param>
        /// <returns>流程汇总删除</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/15</create>
        /// <updata>null</updata>

        public void DelSummaryProcessList(int pk_Process)
        {
            List<Process_List> ddd = db.Set<Process_List>().ToList();
            //首先查出父级单个
            Process_List pl = db.Set<Process_List>().SingleOrDefault(n => n.pk_Process == pk_Process);
            //再通过父级的id查询多个
            List<Process_List> ProcessList = db.Set<Process_List>().Where(n => n.ParentID == pk_Process).ToList();
            ProcessList.Add(pl);


            db.Set<Process_List>().RemoveRange(ProcessList);

        }
        public bool SaveChanges()
        {
            return db.SaveChanges() > 0;
        }
        #endregion

        #region 查看页面根据pk_Process查询
        /// <summary>
        /// 查看页面根据pk_Process查询
        /// </summary>
        /// <param name="id">pk_Process</param>
        /// <returns>查看页面</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/17</create>
        /// <updata>null</updata>

        public object ProcessDataBinding(int pk_Process)
        {
            return (from ProcessList in db.Set<Process_List>()
                    join UserList in db.Set<User_List>() on ProcessList.fk_User equals UserList.pk_User
                    join SysTypeID in db.Set<Sys_TypeID>() on ProcessList.TypeID equals SysTypeID.ID
                    where ProcessList.pk_Process == pk_Process && SysTypeID.TableName.Equals("Process_List")
                    select new
                    {
                        ProcessList.pk_Process,
                        UserList.ImagePath1,
                        SysTypeID.TypeName_CHS,
                        ProcessList.Description,
                        UserList.FullName,
                        ProcessList.CreateDate,
                        ProcessList.Title,
                    }).FirstOrDefault();
        }

        public IEnumerable<object> ProcessViewBinding(int pk_Process)
        {

            return (from ProcessList in db.Set<Process_List>()
                    join UserList in db.Set<User_List>() on ProcessList.CheckUser equals UserList.pk_User
                    join UserDepartment in db.Set<User_Department>() on ProcessList.CheckUser equals UserDepartment.pk_Department
                    join SysStatusID in db.Set<Sys_StatusID>() on ProcessList.StatusID equals SysStatusID.ID
                    where ProcessList.ParentID == pk_Process && SysStatusID.TableName.Equals("Process_List")
                    select new
                    {
                        UserDepartment.DepartmentName,
                        UserList.FullName,
                        UserList.ImagePath1,
                        SysStatusID.StatusName_CHS,
                        SysStatusID.Visual,
                        ProcessList.CreateDate,
                        ProcessList.Note,
                    }).ToList();
        }
        #endregion

        #region 流程审核
        /// <summary>
        /// 流程审核
        /// </summary>
        /// <param name="id">pk_Process</param>
        /// <returns>流程审核</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/18</create>
        /// <updata>null</updata>

        //同意
        public void ProcessAgree(int pk_Process, int CheckUser, int StatusID, string Note)
        {
            Process_List PList = db.Set<Process_List>().SingleOrDefault(n => n.ParentID == pk_Process && n.CheckUser == CheckUser);

            PList.StatusID = StatusID;
            PList.Note = Note;
            PList.ModifyUser = CheckUser;
            PList.ModifyDate = DateTime.Now;
            PList.IsCheck = true;
            PList.CheckDepartment = db.Set<User_List>().SingleOrDefault(n => n.pk_User == CheckUser).fk_Department;

            db.Set<Process_List>().AddOrUpdate(PList);

            List<int?> PList2 = db.Set<Process_List>().Where(n => n.ParentID == pk_Process).Select(n => n.StatusID).ToList();

            Process_List PList3 = db.Set<Process_List>().SingleOrDefault(n => n.pk_Process == pk_Process);

            foreach (var item in PList2)
            {
                if (item == 1)
                {
                    PList3.StatusID = 1;
                    db.Set<Process_List>().AddOrUpdate(PList3);
                }
                else if (item==0)
                {
                    PList3.StatusID = 0;
                    db.Set<Process_List>().AddOrUpdate(PList3);
                }
                else
                {
                    PList3.StatusID = 2;
                    db.Set<Process_List>().AddOrUpdate(PList3);
                }
            }     
        }

        //驳回
        public void ProcessRejected(int pk_Process, int CheckUser, int StatusID, string Note)
        {
            Process_List PList = db.Set<Process_List>().SingleOrDefault(n => n.ParentID == pk_Process && n.CheckUser == CheckUser);

            PList.StatusID = StatusID;
            PList.Note = Note;
            PList.ModifyUser = CheckUser;
            PList.ModifyDate = DateTime.Now;
            PList.IsCheck = true;
            PList.CheckDepartment = db.Set<User_List>().SingleOrDefault(n => n.pk_User == CheckUser).fk_Department;

            db.Set<Process_List>().AddOrUpdate(PList);
        }
        #endregion

        #region 是否禁用按钮
        /// <summary>
        /// 是否禁用按钮
        /// </summary>
        /// <param name="id">pk_Process,CheckUser</param>
        /// <returns>流程审核</returns>
        /// <author>刘俊杰</author>
        /// <create>2020/8/18</create>
        /// <updata>null</updata>
        public int IsButton(int pk_Process, int CheckUser)
        {
            return int.Parse(db.Set<Process_List>().SingleOrDefault(n => n.ParentID == pk_Process && n.CheckUser == CheckUser).StatusID.ToString());
        }
        #endregion
    }
}
